"""
Case Type   : 共享存储-系统内部工具-dsscmd
Case Name   : dsscmd工具查询LUN/注册信息
Create At   : 2023/4/12
Owner       : opentestcase019
Description :
    1.dsscmd工具查询LUN/注册信息，指定-t inq_type参数为有效值
    2.dsscmd工具查询LUN/注册信息，指定-t inq_type参数为无效值
    3.在集群停止的情况下执行
Expect      :
    1.成功
    2.合理报错
    3.显示成功
History     :
"""
import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class ShareStorageTool(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_node = Node('PrimaryDbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.common = Common()
        self.constant = Constant()

    def test_sharestorage_tool(self):
        step1 = "-----step1:dsscmd工具查询LUN/注册信息，指定-t inq_type参数" \
                "为有效值;expect:显示成功-----"
        self.logger.info(step1)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd inq -t lun;dsscmd inq -t reg"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        exp = "Succeed to inquiry LUN information, or inquiry reservations."
        self.assertEquals(2, execute_msg.count(exp), "执行失败" + step1)

        step2 = "-----step2:dsscmd工具查询LUN/注册信息，指定-t inq_type参数为" \
                "无效值;expect:合理报错-----"
        self.logger.info(step2)
        cmd_list = ['-t LUN', '-t REG', '-t reservationss', 'reg', 'lun']
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd inq {cmd}"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(self.primary_node,
                                                    execute_cmd)
            if cmd == 'reg' or cmd == 'lun':
                self.assertIn(f"{cmd} should begin with -.",
                              execute_msg, "执行失败" + step2)
            else:
                self.assertIn("The show type should be [lun|reg].",
                              execute_msg, "执行失败" + step2)

        step3 = "-----step3:在集群停止的情况下执行;expect:显示成功-----"
        self.logger.info(step3)
        stop_msg = self.primary_sh.exec_cm_ctl(mode='stop')
        self.logger.info(stop_msg)
        self.assertIn(self.constant.cm_stop_success_msg,
                      stop_msg, '执行失败' + step3)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd inq -t lun;dsscmd inq -t reg"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertEquals(2, execute_msg.count(exp), "执行失败" + step3)

    def tearDown(self):
        self.logger.info("-----恢复环境-----")
        start_msg = self.primary_sh.exec_cm_ctl(mode='start')
        self.logger.info(start_msg)
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        self.assertIn(self.constant.cm_start_success_msg,
                      start_msg, '启动集群失败')
        self.assertIn('cluster_state   : Normal', status, '执行失败')
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
